//----------------------------------Include------------------------------------
#include "mcu.h"
//-----------------------------------------------------------------------------

//------------------------------------Type-------------------------------------
typedef struct{
  uint32_t  aclk;
  uint32_t  mclk;
  uint32_t  hsmclk;
  uint32_t  smclk;
  uint32_t  bclk;
}t_clk;
//-----------------------------------------------------------------------------

//-----------------------------------Macro-------------------------------------
//内部时钟
#define DCO_CLK              (3000000)     //default 3MHz, max 48MHz
#define VLO_CLK                 (9400)
#define REFO_CLK               (32768)     //32768 or 128KHz
#define MOD_CLK             (25000000)
#define SYS_OSC              (5000000)
//外部时钟
#define LFXT_CLK               (32768)
#define HFXT_CLK            (48000000)     //1~48MHz
//-----------------------------------------------------------------------------

//--------------------------------Static Var-----------------------------------
static t_clk                 nt_clk;
//-----------------------------------------------------------------------------

//--------------------------------Public Func----------------------------------
void mcu_sys_clock(void)
{
//use HFXT 48MHz
#if 1
//配置时钟引脚
//PJ.2--HFXOUT  PJ.3--HFXIN
//PJ.1--LFXOUT  PJ.0--LFXIN
  GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_PJ, GPIO_PIN3 | GPIO_PIN2, GPIO_PRIMARY_MODULE_FUNCTION);
  GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_PJ, GPIO_PIN0 | GPIO_PIN1, GPIO_PRIMARY_MODULE_FUNCTION);  //低速时钟初始化比较慢
  
//根据外接振荡器频率，为内部变量赋值
  CS_setExternalClockSourceFrequency(LFXT_CLK, HFXT_CLK);
  
//Change VCORE to 1 to support the 48MHz frequency
  PCM_setCoreVoltageLevel(PCM_VCORE1);
  
//更改闪存控制器使用的等待状态数用于读取操作。
//当改变时钟的频率范围时，必须使用此函数以允许可读闪存
//通俗来讲就是CPU跑太快了，Flash跟不上，让CPU等等它
  FlashCtl_setWaitState(FLASH_BANK0, 1);
  FlashCtl_setWaitState(FLASH_BANK1, 1);
  
  //这两者不能太靠前，靠前进入晶振启动不起来造成死循环
  CS_startHFXT(false);            //晶体--旁路模式参数设为false
  CS_startLFXT(CS_LFXT_DRIVE3);   //驱动等级3
  
//各时钟使能、分频
  CS_initClockSignal(CS_MCLK,   CS_HFXTCLK_SELECT, CS_CLOCK_DIVIDER_1);  //MCLK    48MHz
  CS_initClockSignal(CS_SMCLK,  CS_HFXTCLK_SELECT, CS_CLOCK_DIVIDER_1);  //SMCLK   48MHz
  CS_initClockSignal(CS_HSMCLK, CS_HFXTCLK_SELECT, CS_CLOCK_DIVIDER_1);  //HSMCLK  48MHz
  
  CS_initClockSignal(CS_ACLK  , CS_LFXTCLK_SELECT, CS_CLOCK_DIVIDER_1);  //ACLK    32768Hz
  CS_initClockSignal(CS_BCLK  , CS_LFXTCLK_SELECT, CS_CLOCK_DIVIDER_1);  //BCLK    32768Hz
//CS_initClockSignal(CS_ACLK  , CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_1);  //ACLK    使用内部低速晶振  时钟源为CS_REFOCLK_SELECT
//CS_initClockSignal(CS_BCLK  , CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_1);  //BCLK
#endif
  
  SystemCoreClockUpdate();
  
  nt_clk.aclk   = CS_getACLK();
  nt_clk.mclk   = CS_getMCLK();
  nt_clk.hsmclk = CS_getHSMCLK();
  nt_clk.smclk  = CS_getSMCLK();
  nt_clk.bclk   = CS_getBCLK();
  
//pin58--P4.2--ACLK
//pin59--P4.3--MCLK
//pin60--P4.4--HSMCLK
}

uint32_t  mcu_get_aclk(void)   { return nt_clk.aclk;   }
uint32_t  mcu_get_mclk(void)   { return nt_clk.mclk;   }
uint32_t  mcu_get_hsmclk(void) { return nt_clk.hsmclk; }
uint32_t  mcu_get_smclk(void)  { return nt_clk.smclk;  }
uint32_t  mcu_get_bclk(void)   { return nt_clk.bclk;   }
//-----------------------------------------------------------------------------

//------------------------------------EOF--------------------------------------


